package com.isoft.demo01.controller;

import com.isoft.demo01.bean.ResponseData;
import com.isoft.demo01.bean.ResultCode;
import com.isoft.demo01.entity.User;
import com.isoft.demo01.service.UserManageService;
import com.isoft.demo01.service.impl.UserManageServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.Size;
import java.util.List;


@CrossOrigin
@RestController
@RequestMapping("user")
public class UserManageController {

    @Autowired
    private UserManageServiceImpl userManageServiceImpl;

    @GetMapping
    public ResponseData getUserList() {
        ResponseData req=new ResponseData();
       List<User> users= userManageServiceImpl.listUser();
       if (users!=null){
           req.setErrorCode(ResultCode.SUCCESS.getCode());
           req.setMsg(ResultCode.SUCCESS.getMessage());
       }else {
           req.setMsg(ResultCode.COMMON_FAIL.getMessage());
           req.setErrorCode(ResultCode.COMMON_FAIL.getCode());
       }
       req.setData(users);
       return req;
    }

//    @PostMapping(value = "/insert")
//    public ResponseData insert(User user) {
//        Boolean success = userManageService.insertUser(user);
//        return getRep(success);
//    }

        @PutMapping("{userid}")
    public ResponseData update(@PathVariable("userid") Integer userid,String username,String email,String phone) {
            ResponseData req=new ResponseData();
        boolean user1=userManageServiceImpl.updateUser(userid,username, email, phone);
        if (user1){
            req.setErrorCode(ResultCode.SUCCESS.getCode());
            req.setMsg(ResultCode.SUCCESS.getMessage());
        }else {
            req.setErrorCode(ResultCode.COMMON_FAIL.getCode());
            req.setMsg(ResultCode.COMMON_FAIL.getMessage());
        }


        req.setData(user1);
        return req;
    }

    @DeleteMapping("/del/{userid}")
    public ResponseData del(@PathVariable("userid") Integer userid) {
        ResponseData req=new ResponseData();
       boolean success = userManageServiceImpl.delete(userid);
       if (success){
           req.setErrorCode(ResultCode.SUCCESS.getCode());
           req.setMsg(ResultCode.SUCCESS.getMessage());
       }else {
           req.setErrorCode(ResultCode.COMMON_FAIL.getCode());
           req.setMsg(ResultCode.COMMON_FAIL.getMessage());
       }
       req.setData(success);
        return req;
    }

}
